from selenium import webdriver


class huya(object):

    def __init__(self):
        self.url = 'https://www.huya.com/l'
        self.driver = webdriver.Chrome()

    def parse_data(self):
        room_list = self.driver.find_elements_by_xpath('//*[@id="js-live-list"]/li')
        # print(len(room_list))
        data_list = []
        for room in room_list:
            temp = {'title': room.find_element_by_xpath('./a[2]').text,
                    'type': room.find_element_by_xpath('./span/span[2]/a').text,
                    'owner': room.find_element_by_xpath('./span/span[1]/i').text,
                    'num': room.find_element_by_xpath('./span/span[3]/i[2]').text,
                    'png': room.find_element_by_xpath('./a[1]/img').get_attribute('src')}
            # print(temp)
            data_list.append(temp)
        return data_list

    def save(self, data_list):
        for data in data_list:
            print(data)

    def run(self):
        # get
        self.driver.get(self.url)
        while True:
            # parse
            data_list = self.parse_data()
            # save
            self.save(data_list)
            # next
            try:
                el_next = self.driver.find_element_by_xpath('//*[@id="laypage_3"]/a[8]')
                self.driver.execute_script('scrolloTo(0,1000000)')
                el_next.click()
            except:
                break


if __name__ == '__main__':
    huya().run()
